package com.yarolegovich.lovelydialog;

import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * Created by yarolegovich on 16.04.2016.
 * If null is passed instead on click listener - dialog will be just closed on click.
 */
@SuppressWarnings("WeakerAccess")
public class LovelyStandardDialog extends AbsLovelyDialog<LovelyStandardDialog> {
    public static final int POSITIVE_BUTTON = ResourceTable.Id_btn_yes;
    public static final int NEGATIVE_BUTTON = ResourceTable.Id_btn_no;
    public static final int NEUTRAL_BUTTON = ResourceTable.Id_btn_neutral;
    private Button positiveButton;
    private Button negativeButton;
    private Button neutralButton;

    public LovelyStandardDialog(Context context) {
        super(context);
    }

    public LovelyStandardDialog(Context context, int theme) {
        super(context, theme);
    }

    public LovelyStandardDialog(Context context, ButtonLayout buttonLayout) {
        super(context, 0, buttonLayout.layoutRes);
    }

    public LovelyStandardDialog(Context context, int theme, ButtonLayout buttonLayout) {
        super(context, theme, buttonLayout.layoutRes);
    }


    {
        positiveButton = findView(ResourceTable.Id_btn_yes);
        negativeButton = findView(ResourceTable.Id_btn_no);
        neutralButton = findView(ResourceTable.Id_btn_neutral);
    }

    public LovelyStandardDialog setPositiveButton(int text, Component.ClickedListener listener) {
        return setPositiveButton(string(text), listener);
    }

    public LovelyStandardDialog setPositiveButton(String text, Component.ClickedListener listener) {
        positiveButton.setVisibility(Component.VISIBLE);
        positiveButton.setText(text);
        positiveButton.setClickedListener(new ClickListenerDecorator(listener, true));
        return this;
    }

    public LovelyStandardDialog setNegativeButtonText(int text) {
        return setNegativeButton(string(text), null);
    }

    public LovelyStandardDialog setNegativeButtonText(String text) {
        return setNegativeButton(text, null);
    }

    public LovelyStandardDialog setNegativeButton(int text, Component.ClickedListener listener) {
        return setNegativeButton(string(text), listener);
    }

    public LovelyStandardDialog setNegativeButton(String text, Component.ClickedListener listener) {
        negativeButton.setVisibility(Component.VISIBLE);
        negativeButton.setText(text);
        negativeButton.setClickedListener(new ClickListenerDecorator(listener, true));
        return this;
    }

    public LovelyStandardDialog setNeutralButtonText(int text) {
        return setNeutralButton(string(text), null);
    }

    public LovelyStandardDialog setNeutralButtonText(String text) {
        return setNeutralButton(text, null);
    }

    public LovelyStandardDialog setNeutralButton(int text, Component.ClickedListener listener) {
        return setNeutralButton(string(text), listener);
    }

    public LovelyStandardDialog setNeutralButton(String text, Component.ClickedListener listener) {
        neutralButton.setVisibility(Component.VISIBLE);
        neutralButton.setText(text);
        neutralButton.setClickedListener(new ClickListenerDecorator(listener, true));
        return this;
    }

    public LovelyStandardDialog setButtonsColor(int color) {
        positiveButton.setTextColor(new Color(color));
        negativeButton.setTextColor(new Color(color));
        neutralButton.setTextColor(new Color(color));
        return this;
    }

    public LovelyStandardDialog setButtonsColorRes(int colorRes) {
        return setButtonsColor(color(colorRes));
    }

    public LovelyStandardDialog setOnButtonClickListener(Component.ClickedListener listener) {
        return setOnButtonClickListener(true, listener);
    }

    public LovelyStandardDialog setOnButtonClickListener(boolean closeOnClick, Component.ClickedListener listener) {
        Component.ClickedListener clickHandler = new ClickListenerDecorator(listener, closeOnClick);
        positiveButton.setClickedListener(clickHandler);
        neutralButton.setClickedListener(clickHandler);
        negativeButton.setClickedListener(clickHandler);
        return this;
    }

    public LovelyStandardDialog setPositiveButtonText(int text) {
        return setPositiveButton(string(text), null);
    }

    public LovelyStandardDialog setPositiveButtonText(String text) {
        return setPositiveButton(text, null);
    }

    public LovelyStandardDialog setPositiveButtonColor(int color) {
        positiveButton.setTextColor(new Color(color));
        return this;
    }

    public LovelyStandardDialog setNegativeButtonColor(int color) {
        negativeButton.setTextColor(new Color(color));
        return this;
    }

    public LovelyStandardDialog setNeutralButtonColor(int color) {
        neutralButton.setTextColor(new Color(color));
        return this;
    }

    public LovelyStandardDialog setPositiveButtonColorRes(int colorRes) {
        return setPositiveButtonColor(color(colorRes));
    }

    public LovelyStandardDialog setNegativeButtonColorRes(int colorRes) {
        return setNegativeButtonColor(color(colorRes));
    }

    public LovelyStandardDialog setNeutralButtonColorRes(int colorRes) {
        return setNeutralButtonColor(color(colorRes));
    }

    @Override
    protected int getLayout() {
        return ButtonLayout.HORIZONTAL.layoutRes;
    }

    public enum ButtonLayout {
        HORIZONTAL(ResourceTable.Layout_dialog_standard),
        VERTICAL(ResourceTable.Layout_dialog_standard_vertical);
        final int layoutRes;

        ButtonLayout(int layoutRes) {
            this.layoutRes = layoutRes;
        }
    }
}
